package ui.views;


import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.views.properties.IPropertySheetPage;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ui.filter.StringArrayFilter;
import ui.listener.ViewActivateEvent;
import ui.provider.PortfolioContent;
import ui.provider.PortfolioLabel;
import ui.util.IPartId;

public class PortfolioView extends ViewPart  
			implements	ITabbedPropertySheetPageContributor {
	
	
	public final static Logger logger = LoggerFactory.getLogger(PortfolioView.class);
	
	private StringArrayFilter filter;
	private TreeViewer treeViewer;

	public void createPartControl(Composite parent) {
		
		treeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

		treeViewer.setContentProvider(new PortfolioContent());
		treeViewer.setLabelProvider(new PortfolioLabel());
// Expand the tree
		treeViewer.setAutoExpandLevel(1);
// Provide the input to the ContentProvider, every class except null
		treeViewer.setInput(treeViewer);
		

// Selection Provider
		getSite().setSelectionProvider(treeViewer);
//		treeViewer.expandAll();
		
// Add Event		
		treeViewer.addPostSelectionChangedListener(new ViewActivateEvent(getSite().getPage(), IPartId.Portfolio,IPartId.PortPolioProp));
//		
//		treeViewer.addSelectionChangedListener(new ISelectionChangedListener(){
//			public void selectionChanged(SelectionChangedEvent event){
//				Object selection = ((IStructuredSelection)event.getSelection()).getFirstElement();
//				if(selection instanceof EgnPortfolio){
//					getSite().getPage().activate(getSite().getPage().findView(propId));
//				}
//				for( IViewReference vr : getSite().getPage().getViewReferences()){
//					logger.debug("view:{}",vr.getId());
//					if(vr.getId().contains("PortfolioProperty")){
//						getSite().getPage().activate(getSite().getPage().findView(vr.getId()));
//						break;
//					}
//				}
//			}
//		});
			
	}

	@Override
	public void setFocus() {
		treeViewer.getControl().setFocus();
	}

	// For Tabbed Property
	@Override
	public Object getAdapter(Class adapter) {
		if (adapter == IPropertySheetPage.class){
			logger.debug("getAdapter Tabbed:{}" );
			return new TabbedPropertySheetPage(this);
		}
		return super.getAdapter(adapter);
	}
	@Override
	public String getContributorId() {
//		logger.debug("contributor Id:{}" , getSite().getId());
		return getSite().getId();
	}
}
